﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AppLayer;
using SpotItComponents;
using SpotItServerAPI;
using Vitruvian.Services;

namespace SpotIt
{
    /// <summary>
    /// Interaction logic for Spotit_Multi_lobby.xaml
    /// 
    /// Richard: this is where most of the magic is going to happen. feel free to engineer the components and access the api here 
    /// </summary>
    public partial class Spotit_Multi_lobby : Window
    {
        //these private variable should enable you to access any API's that you need
        Session session;
        Services services;//review the services code in appLayer. we will most likely use the GameManager property of services exesively in this class
        List<Game> games;
        AudioFiles theAudioFiles = AudioFiles.Instance;
        public Spotit_Multi_lobby(Session session, Services services)
        {
            InitializeComponent();
            this.session = session;
            this.services = services;

            gameDataGrid.CanUserAddRows = false;
            gameDataGrid.CanUserDeleteRows = false;
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            OperationResult result = services.gameManager.AvailableGames(session);
            if (result.Success)
                games = result.Data as List<Game>;

            gameDataGrid.ItemsSource = games;
        }

        private void createButton_Click(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
            SpotIt_Multi_CreateGame createGameWindow = new SpotIt_Multi_CreateGame(session, services);
            createGameWindow.ShowDialog();

            if (!createGameWindow.canceled)
            {
                Spotit_Multi_waitroom waitroomWindow = new Spotit_Multi_waitroom(session, services, createGameWindow.myGameMonitor);
                this.Hide();
                waitroomWindow.ShowDialog();
                this.Close();//I'm closing this window,because the control flow is buggy, so this is the best I could do so that two windows don't appear
            }
        }

        private void joinGameButton_Click(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
        }

        private void mainMenuButton_Click(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
            this.Close();
        }

        
    }
}
